﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using BSC;
using EmrDatastructure;
using MvcPaging;

namespace MvcEmr.Controllers
{
    [Authorize]
    public class DemographicsController : Controller
    {
        private IEmrService _service;

        public DemographicsController()
        {
            _service = DALFactory.DataAccess.CreateDataAccessLayer();
        }
        public ActionResult Index(int? id)
        {
            EmrDemographicSearch r = (EmrDemographicSearch)System.Web.HttpContext.Current.Session["Search"];

            var pageIndex = id ?? 0;
            int pageSize = 3;
            var results = _service.SearchList(r.orLastName, r.orFirstName, r.orDateOfBirth);

            List<EmrDatastructure.EmrDemographic> listE = new List<EmrDatastructure.EmrDemographic>();
            for (int j = pageIndex * pageSize; (j < pageIndex * pageSize + pageSize) && (j < results.Count); j++)
            {
                listE.Add(results[j]);
            }
            PageOfList<EmrDatastructure.EmrDemographic> ipage = new PageOfList<EmrDatastructure.EmrDemographic>(listE, pageIndex, pageSize, results.Count);
            return View("Index", ipage);
        }
        
        //[HttpPost]
        public ActionResult SelectPatient(string id)
        {
            EmrDemographic r = _service.ReadEmrDemographic(id);
            System.Web.HttpContext.Current.Session["DemographicID"] = id;
            System.Web.HttpContext.Current.Session["PatientInfo"] = r.LastName + "," + r.FirstName;
            return RedirectToAction("Index", "Problems");
        }
        //
        // GET: /Demographics/Create
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Demographics/Create
        [HttpPost]
        public ActionResult Create(EmrDemographic r)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return View();
                }
                _service.CreateEmrDemographic(r);
                return RedirectToAction("Index", "Home");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message); 
                return View();
            }
        }

        public ActionResult Edit(string id)
        {
            EmrDemographic r = _service.ReadEmrDemographic(id);
            return View("Edit", r);
        }

        //
        // POST: /Demographics/Edit/5
        [HttpPost]
        public ActionResult Edit(string id, EmrDemographic r)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return View("Edit", r);
                }
                //if (ModelState.IsValidField("DateOfBirth"))
                //{
                    DateTime old = new DateTime(1900, 1, 1);
                    if (r.DateOfBirth < old || r.DateOfBirth > DateTime.Now)
                    {
                        ModelState.AddModelError("DateOfBirth", "Enter valid date of birth");
                        return View("Edit", r);
                    }
                //}

                _service.UpdateEmrDemographic(r);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message); 
                return View("Edit", r);
            }
        }
        // GET: /Demographics/Delete/5
        public ActionResult Delete(string id)
        {
            EmrDemographic r = _service.ReadEmrDemographic(id);
            return View("Delete", r);
        }

        //
        // POST: /Demographics/Delete/5
        [ActionName("Delete")]
        [HttpPost]
        public ActionResult DeletePost(string id)
        {
            try
            {
                _service.DeleteEmrDemographic(id);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message); 
                return View();
            }
        }
        private void EnumFormCollection(FormCollection formCollection)
        {
            foreach (var key in formCollection.AllKeys)
            {
                var value = formCollection[key];
                Console.WriteLine("");
                if (key == "VersionNumber")
                {
                    byte[] todecode_byte = Convert.FromBase64String(value);
                    Console.WriteLine("");
                }
            }
            foreach (var key in formCollection.Keys)
            {
                var value = formCollection[key.ToString()];
                Console.WriteLine("");
            }
            foreach (var key in Request.Form.AllKeys)
            {
                var value = Request.Form[key];
                Console.WriteLine("");
            }
        }
    }
}
